// pages/selectionPeriod/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentDate: new Date().getTime(),
        minDate: new Date().getTime(),
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            } else if (type === 'month') {
                return `${value}月`;
            }
            return value;
        },




        rowNum: [],
        time: 0,
        month: 0,
        year: 0,
        selectDay: '',
        canSelectDay: [],
        timeInterval: ['0', '0'],
        columnNum: ['日', '一', '二', '三', '四', '五', '六'],
        yearArr: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
    },
    //判断当前年份的二月份是28天还是29天
    checkFebruary() {
        if (this.data.year % 100 === 0) {
            if (this.data.year % 400 === 0) {
                this.data.yearArr[1] = 29;
            }
        } else if (this.data.year % 4 === 0) {
            this.data.yearArr[1] = 29;
        } else {
            this.data.yearArr[1] = 28;
        }
    },
    onInput(event) {
        this.setData({
            currentDate: event.detail
        });
    },

    // 生成当前月数
    getDayNum(year, month) {
        let rowNum = [];
        let newmMonth = month < 10 ? `0${month}` : month;
        let a = this.data.yearArr[month - 1];
        for (let b = 0; b < a; b++) {
            let day = b + 1;
            if (day < 10) {
                day = `0${day}`;
            }
            rowNum.push({
                day: b + 1,
                time: `${year}/${newmMonth}/${day}`,
            });
        }
        let time = new Date(`${year},${newmMonth},01`).getDay();
        console.log(time);
        this.supplementDay(rowNum, time, year, month);

    },

    supplementDay(rowNum, time, year, month) {
        if (time !== 0) {
            for (let f = 0; f < time; f++) {
                rowNum.unshift({
                    day: 0,
                });
            }
        }
        this.data.canSelectDay.push({
            year: year,
            month: month,
            rowNum: rowNum,
        });
        this.setData({
            canSelectDay: this.data.canSelectDay,
        })
        console.log(this.data.canSelectDay);
    },

    goSelectDay(event) {
        console.log(event.currentTarget.dataset.time);
    },
    goBack() {
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        prevPage.setData({
            time: ['06-09', '10-09'],
        })
        wx.navigateBack({ //返回
            delta: 1
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        let year = new Date().getFullYear();
        let month = new Date().getMonth() + 1;

        for (let a = 0; a < 5; a++) {
            if (month + a === 2) {
                this.checkFebruary();
            }
            if (month + a > 12) {
                let newYear = year + 1;
                let newMonth = month + a - 12;
                this.getDayNum(newYear, newMonth);
            } else {
                this.getDayNum(year, month + a);
            }
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})